/*考试试卷页面脚本*/

//选项功能随机
$(function () {
	/*
    $('.question-block').each(function () {
        $(this).find('li').reorder();
    });
    var options = [];
    $('.question-block').find('li').each(function () {
        options.push($(this).attr('data-option'));
    }).end().next('.question-answers').find(':radio,:checkbox').each(function (index) {
        $(this).val(options[index]);
    });
	*/
});

//考生所选答案
var ans = {"paper_id":0,"student_answer":[]};
$(function () {
    //获取当前试卷编号
    ans.paper_id = $('#paper').attr('data-paper-id');

    //当考生选择答案时触发该事件
    $('.question .form-check-label input').click(function () {
        var student_answer = [];
        //考生所做每道题目
        var stu_paper_answer = {"question_id":0,"stu_ans":""};
        var index = $(this).parents('.question').index('.question');

        //获取当前题目编号
        var question_id = $(this).parents('.question').attr('data-question-id');
        //如果该考生没有做过该题目
        if($('.question:eq('+index+') .form-check-label input:checked').length > 0){
            $(this).parents('.question').find('i').removeClass('btn-primary').addClass('btn-success');

            stu_paper_answer.question_id = question_id;
            $('.question:eq('+index+') .form-check-label input:checked').each(function () {
                student_answer.push($(this).val());
            });
            stu_paper_answer.stu_ans = student_answer.join(',');
            if($(this).attr('type')=='radio' && student_answer.length == 1){
                //单选题提交
        		var i = 0;
            	if(ans.student_answer!=null && ans.student_answer.length>0){
            		for(;i < ans.student_answer.length;i++){
            			if(ans.student_answer[i].question_id == stu_paper_answer.question_id){
            				ans.student_answer[i].stu_ans = stu_paper_answer.stu_ans;
            				break;
            			}
            		}
            	}
            	if(i == ans.student_answer.length){
                    ans.student_answer.push(stu_paper_answer);
            	}
            }else if($(this).attr('type')=='checkbox' && student_answer.length==2){
                //多选按钮提交
                //stu_paper_answer.stu_ans = student_answer.join(',');
            	var i = 0;
            	if(ans.student_answer!=null && ans.student_answer.length>0){
            		for(;i < ans.student_answer.length;i++){
            			if(ans.student_answer[i].question_id == stu_paper_answer.question_id){
            				ans.student_answer[i].stu_ans = stu_paper_answer.stu_ans;
            				break;
            			}
            		}
            	}
            	if(i == ans.student_answer.length){
                    ans.student_answer.push(stu_paper_answer);
            	}
            }

        }else{
            $(this).parents('.question').find('i').removeClass('btn-success').addClass('btn-primary');
            for(var i = 0;i < ans.student_answer.length;i++){
                if(ans.student_answer[i].question_id == question_id){
                    ans.student_answer.splice(i,1);//移除元素
                    break;
                }
            }
        }
        $.cookie(student_no,JSON.stringify(ans),{expires:1/24});
        $('.complete_count>span').text(questionCompleteCount());
    });
});

//答题完成数量
function questionCompleteCount() {
    return $('.question_index.btn-success').length;
}

$(function () {
   $('.complete_count').click(questionComplete);
});

//查看当前完成情况
function questionComplete() {
    var html = '<div style="width:700px; margin: 0 auto">';
    for(var i = 1;i<=$('.question_index').length;i++){
        var cls = 'primary';
        if($('.question_index').eq(i-1).attr('class').indexOf('btn-success')>0){
            cls = 'success';
        }
        html+='<a style="color: white;outline:none;text-decoration: none;" href="front/exam.jsp#jump'+i+'"><span style="margin: 5px;width:60px;height:58px;line-height: 43px" class="btn btn-'+cls+'">'+i+'</span></a>';
    }
    html+='</div>';
    //$('#completeModal').find('.modal-body').html(html);
    //$('#completeModal').modal();
    swal({
        html:html,
        width:'800px',
        showConfirmButton:false,
    });

}

function jiaojuan(){
	swal({
        title: '提示',
        text: '确认交卷吗?',
        type: 'info',
        confirmButtonText: '确认',
        cancelButtonText: "我再想想"
    }).then(function (isConfirm) {
        if(isConfirm){
        	//判断一下用户是否已经考试过
            $("#examForm").trigger("submit");
        }
    });
}

//用户交卷
function submit_paper() {
    if($.cookie(student_no) == undefined || parseInt($('.complete_count>span').text()) == 0){
       swal("你还没有答题,请认真对待考试");
       return;
    }
    swal({
        title: '提示',
        text: '确认交卷吗?',
        type: 'info',
        confirmButtonText: '确认',
        cancelButtonText: "我再想想"
    }).then(function (isConfirm) {
        if(isConfirm){
            $.post('front/examPaperAnswer/addAnswer',{"str":$.cookie(student_no)},function (data) {
                $.cookie(student_no,null);
                location.href = data.url+"/"+$('#paper').attr('stu_paper_id');
            });
        }
    });
}